{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "307804a3-c02b-4a57-ac0d-172c30ddc851",
   "metadata": {},
   "source": [
    "# Qdrant Vector Store"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7010b1d-d1bb-4f08-9309-a328bb4ea396",
   "metadata": {},
   "source": [
    "#### Creating a Qdrant client"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d5527d3d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-17T08:43:49.782304Z",
     "start_time": "2023-08-17T08:43:45.543872Z"
    }
   },
   "outputs": [],
   "source": [
    "import logging\n",
    "import sys\n",
    "\n",
    "import qdrant_client\n",
    "from IPython.display import Markdown, display\n",
    "from llama_index import (\n",
    "    VectorStoreIndex,\n",
    "    ServiceContext,\n",
    "    SimpleDirectoryReader,\n",
    ")\n",
    "from llama_index.storage.storage_context import StorageContext\n",
    "from llama_index.vector_stores.qdrant import QdrantVectorStore"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07489add",
   "metadata": {},
   "source": [
    "If running this for the first, time, install using this command: \n",
    "\n",
    "```\n",
    "!pip install -U qdrant_client\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b1757e8a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-17T08:43:51.982898Z",
     "start_time": "2023-08-17T08:43:51.974706Z"
    }
   },
   "outputs": [],
   "source": [
    "client = qdrant_client.QdrantClient(\n",
    "    # you can use :memory: mode for fast and light-weight experiments,\n",
    "    # it does not require to have Qdrant deployed anywhere\n",
    "    # but requires qdrant-client >= 1.1.1\n",
    "    location=\":memory:\"\n",
    "    # otherwise set Qdrant instance address with:\n",
    "    # uri=\"http://<host>:<port>\"\n",
    "    # set API KEY for Qdrant Cloud\n",
    "    # api_key=\"<qdrant-api-key>\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "64481526",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-17T08:43:53.192496Z",
     "start_time": "2023-08-17T08:43:53.184344Z"
    }
   },
   "outputs": [],
   "source": [
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ee4473a-094f-4d0a-a825-e1213db07240",
   "metadata": {},
   "source": [
    "#### Load documents, build the VectorStoreIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "68cbd239-880e-41a3-98d8-dbb3fab55431",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-17T08:43:54.631066Z",
     "start_time": "2023-08-17T08:43:54.620544Z"
    }
   },
   "outputs": [],
   "source": [
    "# load documents\n",
    "documents = SimpleDirectoryReader(\"../data/paul_graham\").load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ba1558b3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-17T08:43:57.169680Z",
     "start_time": "2023-08-17T08:43:55.526708Z"
    }
   },
   "outputs": [],
   "source": [
    "service_context = ServiceContext.from_defaults()\n",
    "vector_store = QdrantVectorStore(client=client, collection_name=\"paul_graham\")\n",
    "storage_context = StorageContext.from_defaults(vector_store=vector_store)\n",
    "index = VectorStoreIndex.from_documents(\n",
    "    documents, storage_context=storage_context, service_context=service_context\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04304299-fc3e-40a0-8600-f50c3292767e",
   "metadata": {},
   "source": [
    "#### Query Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "35369eda",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-17T08:44:01.471157Z",
     "start_time": "2023-08-17T08:43:58.754776Z"
    }
   },
   "outputs": [],
   "source": [
    "# set Logging to DEBUG for more detailed outputs\n",
    "query_engine = index.as_query_engine()\n",
    "response = query_engine.query(\"What did the author do growing up?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bedbb693-725f-478f-be26-fa7180ea38b2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-17T08:44:02.245397Z",
     "start_time": "2023-08-17T08:44:02.234787Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The author worked on writing and programming outside of school before college. They wrote short stories and tried writing programs on an IBM 1401 computer. They also built a microcomputer kit and started programming on it, writing simple games and a word processor.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "99212d33",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-17T08:44:09.362466Z",
     "start_time": "2023-08-17T08:44:03.342172Z"
    }
   },
   "outputs": [],
   "source": [
    "# set Logging to DEBUG for more detailed outputs\n",
    "query_engine = index.as_query_engine()\n",
    "response = query_engine.query(\"What did the author do after his time at Viaweb?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1a720ad6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-17T08:44:10.239210Z",
     "start_time": "2023-08-17T08:44:10.222263Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>After his time at Viaweb, the author started a new company called Aspra. However, halfway through the summer, he realized that he didn't want to run a company, especially a big one. Instead, he decided to build a subset of the vision as an open-source project. He and his colleague then switched to working on a new dialect of Lisp called Arc. Additionally, the author started publishing essays online and realized that he would always write essays alongside whatever else he did.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b78d7c66",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
